<?php
// +----------------------------------------------------------------------
// |管理员相关操作类
// +----------------------------------------------------------------------
// | Copyright (c) 2010 旋风工作室 All rights reserved.
// +----------------------------------------------------------------------
// | Author：XF.Zhoujz
// +----------------------------------------------------------------------

class CardsAction extends Action{
	public function index(){
		$this->validate();
		import("@.ORG.Page");
		$cardsModel = D("cards");
		$count = $cardsModel->count();
		$p = new Page($count,50);
		$list = $cardsModel->limit($p->firstRow.','.$p->listRows)->order('card_generatedate desc')->findAll();
		$p->setConfig('header','篇记录');
		$p->setConfig('prve','<');
		$p->setConfig('next','>'); 
	    $p->setConfig('first','<<'); 
	    $p->setConfig('last','>>'); 
		$page = $p->show();
		$this->assign("nowpage",!empty($_GET["p"])?$_GET["p"]:1);
		$this->assign("page", $page); 
	    $this->assign("list", $list); 
	    $this->display('index');
	}
	
	//用户查询
	public function Search(){
		$this->validate();
		import("@.ORG.Page");
		$cardsModel = D("cards");
		if(strlen(trim($_POST['card_id']))!=0){
			$map['card_id'] = array("like","%".trim($_POST['card_id'])."%");	
		}
		if(strlen(trim($_POST['card_amount']))!=0 && trim($_POST['card_amount'] !="0")){
			$map['card_amount'] = array("eq",trim($_POST['card_amount']));
		}
		if(strlen(trim($_POST['card_static']))!=0 && trim($_POST['card_static'] !="3")){
			$map['card_static'] = array("eq",trim($_POST['card_static']));
		}
		$count = $cardsModel->where($map)->count();
		$p = new Page($count,50);
		$list = $cardsModel->limit($p->firstRow.','.$p->listRows)->order('card_generatedate desc')->where($map)->findAll();
		$p->setConfig('header','篇记录');
		$p->setConfig('prve','<');
		$p->setConfig('next','>'); 
	    $p->setConfig('first','<<'); 
	    $p->setConfig('last','>>'); 
	    $page = $p->show(); 
	    $this->assign("nowpage",!empty($_GET["p"])?$_GET["p"]:1);
		$this->assign("page", $page); 
	    $this->assign("list", $list);
	    $this->assign("post",$_POST?$_POST:$_GET); 
	    $this->display('index');
	}
	
	//删除充值卡
	public function delete(){
		$this->validate();
		$cardModel = D('cards');
		$id = $_REQUEST['id'];
		if (isset ($id)) {
			$condition = array ('card_id' => array ('in', explode ( ',', $id ) ) );
			if (false !== $cardModel->where ( $condition )->delete ()) {
			} else {
				$errmsg = $cardModel->getDbError();
			}
		}else{
			$errmsg = $cardModel->getError();
		}
		$this->assign('errmsg',$errmsg);
		$this->redirect('Cards/index');
		
	}
	
	//生成充值卡
	public function createCards(){
		$this->validate();
		import("@.ORG.Page");
		set_time_limit(0); 
		$index = count($_POST)/2;
		$condetion = Array();
		for($i=0;$i<$index;$i++){
			$condetion['amount'][] = $_POST["amount".($i+1)];
			$condetion['num'][] = $_POST["num".($i+1)];
		}
		$cardModel = D('Cards');
		$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$len=8;
		$data = array();
		for($count=0;$count<$index;$count++){
			for($i=0;$i<trim($condetion['num'][$count]);$i++){
				$key_st="";
				$key_ed="";
				$pass="";
				for($j=0;$j<2;$j++)
				{
				   $key_st .= $chars{mt_rand(0,35)}; 
				}
				for($j=0;$j<3;$j++)
				{
				   $key_ed .= $chars{mt_rand(0,35)};
				}
				for($j=0;$j<6;$j++)
				{
				   $pass .= $chars{mt_rand(0,35)};
				}
				$data["card_id"] = "Y".strtoupper($key_st).date("mdHis").strtoupper($key_ed);
				$data["card_password"]=strtoupper($pass);
				$data["card_amount"]=trim($condetion['amount'][$count]);
				$data["card_static"]="0";
				$data["card_userid"]="";
				$data["card_paiddate"]="";
				$data["card_generatedate"]=date("Y-m-d H:i:s");
				if($vo = $cardModel->create()){
					if($cardModel->add($data)){
					}else{
						$errmsg = $cardModel->getDbError();
					 	$this->assign('errmsg',$errmsg);
						$this->assign('list',$list);
						$this->assign('vo',$_POST);
						$this->display('add');
					}
				}else{
					$errmsg = $cardModel->getError();
					$this->assign('errmsg',$errmsg);
					$this->assign('list',$list);
					$this->assign('vo',$_POST);
					$this->display('add');
				}
			}
		}
		$this->redirect('Cards/index');
	}
	
	//导出csv
	public function outCsv(){
		$this->validate();
		import("@.ORG.Csvutil");
		import("@.ORG.Page");
		$cardsModel = D("cards");
		$title = array("cards","password","amount","date");
		//$title = array("11","12","14","13");
		if($_POST['card_id_c']){
			$result[] = explode(",",$_POST['card_id_c']);
			$result[] = explode(",",$_POST['card_password_c']);
			$result[] = explode(",",$_POST['card_amount_c']);
			$result[] = explode(",",$_POST['card_generatedate_c']);
			for($i=0;$i<count($result[0]);$i++){
				$remp[] = array($result[0][$i],$result[1][$i],$result[2][$i],$result[3][$i]); 
				$vo = $cardsModel->where("card_id='".$result[0][$i]."'")->field('card_static')->find();
				if($vo){
					if($vo['card_static']==='0'){
						$data['card_static'] = '1';
						$cardsModel->where("card_id='".$result[0][$i]."'")->save($data);
					}
				}
			}
		}else{
			if(strlen(trim($_POST['card_id']))!=0){
				$map['card_id'] = array("like","%".trim($_POST['card_id'])."%");	
			}
			if(strlen(trim($_POST['card_amount']))!=0 && trim($_POST['card_amount'] !="0")){
				$map['card_amount'] = array("eq",trim($_POST['card_amount']));
			}
			if(strlen(trim($_POST['card_static']))!=0 && trim($_POST['card_static'] !="3")){
				$map['card_static'] = array("eq",trim($_POST['card_static']));
			}
			$remp = $cardsModel->where($map)->field('card_id,card_password,card_amount,card_generatedate')->findAll();
			
		}
		$csv = new CsvUtil("cards",$title,$remp);
		$csv->outputCsv();
	}
	
	//登陆信息验证
	public function validate(){
		if(empty($_SESSION['admin'])){
			$this->redirect('Public/login');
		}
	}
	
}
?>